import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _omit from "lodash/omit";
import _map from "lodash/map";
import _isFunction from "lodash/isFunction";
import React from 'react';
import { Button, Table } from 'antd';
import Upload from "../../Upload";
import intl from "../../../utils/intl";
import { getWidthFromWord } from "../utils";
import { getComponentProps } from "../ComposeForm/utils";
/**
 * 获取计算得来的属性(且耗时多)
 * @param {Object} props
 */

function getNoEditableComputeTableProps(props) {
  var fields = props.fields,
      context = props.context; // let index = 0;

  var columnsWidth = 0;

  var columns = _map(fields, function (field) {
    // const required = field.requiredFlag !== 0;
    var columnWidth = getWidthFromWord({
      word: field.fieldDescription,
      minWidth: 60,
      fontWidth: 12,
      defaultWidth: 100,
      paddingWidth: 36
    });
    columnsWidth += columnWidth;
    var column = {
      dataIndex: field.componentType === 'ValueList' || field.componentType === 'Lov' ? "".concat(field.fieldCode, "Meaning") : field.fieldCode,
      title: field.fieldDescription,
      width: columnWidth
    };

    if (field.componentType === 'Checkbox' || field.componentType === 'Switch') {
      column.render = function (item) {
        return item === 1 ? intl.get('cpaas.common.status.yes').d('是') : intl.get('cpaas.common.status.no').d('否');
      };
    } else if (field.componentType === 'Upload') {
      var uploadProps = getComponentProps({
        field: field,
        componentType: 'Upload',
        context: context
      });

      column.render = function (item) {
        return item && /*#__PURE__*/React.createElement(Upload, Object.assign({}, uploadProps, {
          viewOnly: true,
          attachmentUUID: item
        }));
      };
    }

    return column;
  });

  return {
    scroll: {
      x: columnsWidth
    },
    columns: columns
  };
}

function getNoComputeTableProps(props) {
  var _props$rowKey = props.rowKey,
      rowKey = _props$rowKey === void 0 ? 'id' : _props$rowKey,
      _props$dataSource = props.dataSource,
      dataSource = _props$dataSource === void 0 ? [] : _props$dataSource;
  return {
    rowKey: rowKey,
    dataSource: dataSource
  };
}

var omitProps = ['addable', 'removable', 'editable', 'fields', 'onRef', 'onGetValidateDataSourceHook', 'fieldLabelWidth', 'onRowEdit', 'onAdd', 'onRemove', 'onRowSelectionChange'];

var BaseComposeTable = /*#__PURE__*/function (_React$PureComponent) {
  _inherits(BaseComposeTable, _React$PureComponent);

  var _super = _createSuper(BaseComposeTable);

  function BaseComposeTable(props) {
    var _this;

    _classCallCheck(this, BaseComposeTable);

    _this = _super.call(this, props);
    _this.state = {
      prevState: {},
      computeTableProps: {}
    };
    _this.handleBtnAddClick = _this.handleBtnAddClick.bind(_assertThisInitialized(_this));
    _this.handleBtnRemoveClick = _this.handleBtnRemoveClick.bind(_assertThisInitialized(_this));
    return _this;
  }

  _createClass(BaseComposeTable, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var onRef = this.props.onRef;

      if (_isFunction(onRef)) {
        onRef(this);
      }
    }
  }, {
    key: "handleBtnAddClick",
    value: function handleBtnAddClick() {
      var onAdd = this.props.onAdd;

      if (_isFunction(onAdd)) {
        onAdd();
      }
    }
  }, {
    key: "handleBtnRemoveClick",
    value: function handleBtnRemoveClick() {
      var onRemove = this.props.onRemove;

      if (_isFunction(onRemove)) {
        onRemove();
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this$state = this.state,
          _this$state$computeTa = _this$state.computeTableProps,
          computeTableProps = _this$state$computeTa === void 0 ? {} : _this$state$computeTa,
          _this$state$noCompute = _this$state.noComputeTableProps,
          noComputeTableProps = _this$state$noCompute === void 0 ? {} : _this$state$noCompute;
      var _this$props = this.props,
          removable = _this$props.removable,
          addable = _this$props.addable,
          onRowSelectionChange = _this$props.onRowSelectionChange,
          _this$props$selectedR = _this$props.selectedRowKeys,
          selectedRowKeys = _this$props$selectedR === void 0 ? [] : _this$props$selectedR;

      var otherProps = _omit(this.props, omitProps);

      var rowSelection = null;
      var buttons = [];

      if (addable) {
        buttons.push( /*#__PURE__*/React.createElement(Button, {
          key: "add",
          onClick: this.handleBtnAddClick
        }, intl.get('cpaas.common.button.add').d('新增')));
      }

      if (removable) {
        buttons.push( /*#__PURE__*/React.createElement(Button, {
          key: "remove",
          onClick: this.handleBtnRemoveClick,
          disabled: selectedRowKeys.length === 0
        }, intl.get('cpaas.common.button.delete').d('删除')));
        rowSelection = {
          selectedRowKeys: selectedRowKeys,
          onChange: onRowSelectionChange
        };
      }

      return /*#__PURE__*/React.createElement(React.Fragment, {
        key: "base-compose-table"
      }, buttons.length > 0 && /*#__PURE__*/React.createElement("div", {
        key: "base-compose-table-options",
        className: "table-list-operator"
      }, buttons), /*#__PURE__*/React.createElement(Table, Object.assign({
        bordered: true,
        pagination: false,
        key: "base-compose-table-table"
      }, otherProps, noComputeTableProps, computeTableProps, {
        rowSelection: rowSelection
      })));
    }
  }], [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(nextProps, prevState) {
      var nextState = {
        prevState: prevState.prevState,
        computeTableProps: prevState.computeTableProps
      };
      var fields = nextProps.fields,
          editable = nextProps.editable,
          addable = nextProps.addable,
          removable = nextProps.removable,
          _nextProps$getCompute = nextProps.getComputeTableProps,
          getComputeTableProps = _nextProps$getCompute === void 0 ? getNoEditableComputeTableProps : _nextProps$getCompute;
      var _prevState$prevState = prevState.prevState,
          prevFields = _prevState$prevState.fields,
          prevEditable = _prevState$prevState.editable,
          prevAddable = _prevState$prevState.addable,
          prevRemovable = _prevState$prevState.removable;

      if (fields !== prevFields || addable !== prevAddable || editable !== prevEditable || removable !== prevRemovable) {
        nextState.computeTableProps = getComputeTableProps(nextProps);
        nextState.prevState.fields = fields;
        nextState.prevState.editable = editable;
        nextState.prevState.addable = addable;
        nextState.prevState.removable = removable;
      }

      nextState.noComputeTableProps = getNoComputeTableProps(nextProps);
      return nextState;
    }
  }]);

  return BaseComposeTable;
}(React.PureComponent);

export { BaseComposeTable as default };